<template>
  <el-dialog
    title="修改密码"
    width="600"
    draggable
    :close-on-press-escape = false
    :close-on-click-modal	= false
    v-model="show"
    @before-close="close"
  >
    <el-form label-width="120px" :model="passwordform" :rules="rules" ref="passwordform">
      <el-form-item label="旧密码:" prop="OldPwd">
        <el-input v-model="passwordform.OldPwd" maxlength="16" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="新密码:" prop="NewPwd">
        <el-input v-model="passwordform.NewPwd" type="password" maxlength="16" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="确认密码:" prop="confirmPassword">
        <el-input v-model="passwordform.confirmPassword"  type="password" maxlength="16" show-word-limit></el-input>
      </el-form-item>
      <div style="display:flex;justify-content: flex-end;">
        <el-button type="primary" icon="Position" @click="submit">
          确定
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import { Header } from 'element-plus/es/components/table-v2/src/components/index.mjs'
import { connrequest } from '../api/connrequest'
export default {
  props: {
    passWorldDialog:{
      type: Boolean,
      default: () => false
    },
  },
  watch:{
    passWorldDialog(value){
      this.show = value
    },
  },
  data(){
    return{
      show: false,
      passwordform:{
        OldPwd: '',
        NewPwd: '',
        confirmPassword: ''
      },
      rules:{
        OldPwd: [ 
          { required: true, message:'旧密码不能为空' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ],
        NewPwd: [ 
          { required: true, message:'新密码不能为空' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ],
        confirmPassword: [ 
          { required: true, message:'确认密码不能为空' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  methods:{
    submit(){
      this.$refs['passwordform'].validate(async (valid) => {
        if (valid) {
          if(this.passwordform.NewPwd === this.passwordform.OldPwd){
            this.$message.error('新密码不能和旧密码一样')
          }else if(this.passwordform.NewPwd !== this.passwordform.confirmPassword){
            this.$message.error('两次密码输入内容不一致')
          }else{
            const data = {
              Data:{
                NewPwd: this.passwordform.NewPwd,
                OldPwd: this.passwordform.OldPwd
              },
              Header: {
                CmdCode: 'ModifyPwdEmployee',
                EmployeeName: sessionStorage.getItem('userName')
              }
            }
            const res = await connrequest('ModifyPwdEmployee', data)
            if(res.Status.StatusCode === 0) {
              this.$alert('密码已经修改，请重新登录', '修改密码', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$emit('submit')
                }
              });
            }
          }
        }
      })
    },
    close(){
      this.$emit('closepassWorldDialog')
    },
  }
}
</script>

<style lang="scss" scoped>

  :deep(.el-input__count-inner) {
    background: none !important;
  }

  .relatedConditions{
    display: flex;
    align-items: center;
    height: 95px;
    .conditionname{
      width: 120px;
      height: 30px;
      line-height: 30px;
      color: #409EFF;
      border: 2px solid #409EFF;
      text-align: center;
      border-radius: 20px;
      margin-right: 20px;
    }
  }
  .dialog-footer{
    display: inline-block;
    display: flex;
    align-items: center;
    padding-bottom: 40px;
    .demo-image{
      display: flex;
      .block{
        width: 300px;
      }
    }
  }
</style>
<style>
.el-dialog__header {
  padding: 0;
  margin: 0;
}
.el-dialog.is-draggable .el-dialog__header{
  border-bottom: 1px solid #409EFF;
}
.el-dialog__title{
  display: inline-block;
  width: 180px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background: #409EFF;
  color: #fff;
}
.rounded-checkbox .el-checkbox__inner {
  border-radius: 50%;
}
.rounded-checkbox .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #409EFF;
  border-color: #409EFF;
}
.el-checkbox__input.is-checked + .el-checkbox__label{
  color: #409EFF;
}
.el-dialog__footer {
  margin: 0;
  padding: 0;
  text-align: left;
  margin-left: 20px;
  font-size: 14px;
}
</style>
